#coding=utf-8
import time

import yaml
from uiautomator2 import xpath


from page_object.guang_page import GuangPage
from resource.path_info import *

"""
创建直播预告
"""
class LivingPage(GuangPage):
    switch_on_index = 1
    add_good_name = ""
    """
    上屏讲解
    """
    def switch_on_goods_showing(self, d):
        global switch_on_index
        print("switch_on_goods_showing")
        d.xpath(living_to_goods_bag_xpath).click()
        bag_price = 0
        goodsName = ""
        for i in range(1,5,1):
            if d.xpath(living_goods_switch_on_xpath %i).exists:
                if d.xpath(living_goods_switch_on_xpath %i).get_text() == "上屏讲解":
                    switch_on_index = i
                    d.xpath(living_goods_switch_on_xpath %i).click()
                    if d.xpath(living_baggoods_is_limit_xpath %i).get_text() == "限时":
                        bag_price = float(d.xpath(living_baggoods_limit_price_xpath % i).get_text())
                    else:
                        bag_price = float(d.xpath(living_baggoods_price_xpath % i).get_text())
                    goodsName = d.xpath(living_baggoods_name_xpath %i).get_text()
                    d.press("back")
                    break
        showing_price = float(d.xpath(living_goods_showing_price_xpath).get_text()[1:])
        assert showing_price == bag_price
        return goodsName

    """
    下屏
    """

    def switch_off_goods_showing(self, d):
        print("switch_off_goods_showing")
        d.xpath(living_to_goods_bag_xpath).click()
        d.xpath(living_goods_switch_on_xpath % switch_on_index).click()
        d.press("back")
        if not d.xpath(living_goods_showing_price_xpath).exists:
            print("下架成功")
        else:
            raise Exception("下架失败")

    def goods_switch_on_off(self, d):
        self.switch_on_goods_showing(d)
        self.switch_off_goods_showing(d)
        goodsName = self.switch_on_goods_showing(d)
        return goodsName

    """
    发送优惠券
    """
    def add_coupon(self, d):
        print("add_coupon")
        d.xpath(living_to_coupon_btn_xpath).click()
        #创建优惠券
        d.xpath(living_addLiving_coupon_xpath).click()
        d.xpath(living_checkbox_coupon_xpath).wait(timeout=3.0)
        d.xpath(living_checkbox_coupon_xpath).click()
        d.xpath(living_add_btn_coupon_xpath).click()
        d.xpath(living_send_btn_coupon_xpath).click()
        d.xpath(living_send_confirm_coupon_xpath).click()
        time.sleep(2)
        if d.xpath(living_send_btn_coupon_xpath).exists:
            d.press("back")
        return self

    """
    开启抽奖
    """
    def add_lottery(self, d, title, number):
        print("add_lottery")
        d.xpath(living_to_lottery_btn_xpath).click()
        d.xpath(living_add_lottery_xpath).click()
        #自定义
        time.sleep(1)
        d.xpath(living_custom_lottery_xpath).wait(timeout=3.0)
        d.xpath(living_custom_lottery_xpath).click()
        d.xpath(living_custom_lottery_input_xpath).click()
        d(focused=True).send_keys(title)
        d.xpath(living_custom_lottery_add_xpath).click()
        time.sleep(1)
        #人数
        d.xpath(living_number_lottery_input_xpath).wait(timeout=3.0)
        d.xpath(living_number_lottery_input_xpath).click()
        d(focused=True).send_keys(number)
        if d.xpath(sougou_input_title_xpath).exists or d.xpath(sougou_input_title_math_xpath).exists:
            d.press("back")
        d.xpath(living_attention_room_lottery_xpath).click()
        if d.xpath(sougou_input_title_xpath).exists or d.xpath(sougou_input_title_math_xpath).exists:
            d.press("back")
        d.xpath(living_add_lottery_sure_xpath).click()
        #推送
        time.sleep(3)
        d.xpath(living_push_lottery_xpath).click()
        #todo - 抽奖或者返回
        return self

    """
    爆款推荐
    """

    def goods_recommand_add(self, d):
        print("goods_recommand_add")
        index = 0
        d.xpath(living_to_goods_bag_xpath).click()
        for i in range(1,3,1):
            if d.xpath(living_baggoods_recommend_btn_xpath % i).info['selected'] == "false":
                index = i
                d.xpath(living_baggoods_recommend_btn_xpath % i).click()
                break
        time.sleep(1)
        assert d.xpath(living_baggoods_recommend_btn_xpath % index).info['selected'] == "true"
        d.press("back")

    """
    推荐理由填写
    """

    def add_goods_recommand_reason(self, d, reasonText):
        print("add_goods_recommand_reason")
        index = 0
        d.xpath(living_to_goods_bag_xpath).click()
        # 输入消息
        d.xpath(living_baggoods_recommend_reason_input_xpath % 1).wait(timeout=3.0)
        d.xpath(living_baggoods_recommend_reason_input_xpath % 1).click()
        d(focused=True).send_keys(reasonText)
        d.press("back")
        time.sleep(1)
        if d.xpath(living_baggoods_recommend_reason_input_xpath % 1).exists:
            d.press("back")
        # 查看推荐理由
        d.xpath(living_to_goods_bag_xpath).click()
        # 输入消息
        time.sleep(1)
        d.xpath(living_recommend_reason_inputted_xpath % 1).wait(timeout=3.0)
        assert d.xpath(living_recommend_reason_inputted_xpath % 1).get_text() == reasonText
        d.press("back")
        time.sleep(1)

    """
    设置神秘价
    """

    def add_goods_mysterious_price(self, d):
        print("add_goods_mysterious_price")
        d.xpath(living_to_goods_bag_xpath).click()
        d.xpath(living_baggoods_manager_btn_xpath).click()
        d.xpath(living_baggoods_manager_to_mysterious_price_xpath).click()
        d.xpath(living_baggoods_mngr_myst_price_enter_xpath % 1).wait(timeout=3.0)
        d.xpath(living_baggoods_mngr_myst_price_enter_xpath % 1).click()
        d.xpath(living_baggoods_mngr_myst_price_confirm_xpath).click()
        d.xpath(living_baggoods_mngr_myst_price_showing_xpath).wait(timeout=3.0)
        if d.xpath(living_baggoods_mngr_myst_price_showing_xpath).exists:
            print("get mysterious price successfully")
            assert "神秘价" == d.xpath(living_baggoods_mngr_myst_price_showing_xpath).get_text()[0:3]
        else:
            raise Exception("get mysterious price fail")
        d.press("back")

    """
    直播间添加商品
    """

    def add_goods_in_broadcast(self, d):
        print("add_goods_broadcast")
        global add_good_name
        d.xpath(living_to_goods_bag_xpath).click()
        time.sleep(1)
        d.xpath(living_baggoods_manager_btn_xpath).click()
        if not d.xpath(living_baggoods_manager_add_goods_xpath).exists:
            d(text="管理").click()
        d.xpath(living_baggoods_manager_add_goods_xpath).wait(timeout=5.0)
        d.xpath(living_baggoods_manager_add_goods_xpath).click()
        # 继续添加
        d.xpath(living_baggoods_manager_to_add_others_xpath).wait(timeout=5.0)
        d.xpath(living_baggoods_manager_to_add_others_xpath).click()
        time.sleep(2)
        # 未绑定的情况
        # if d.xpath(mall_title_xpath).exists:
        #     d.xpath(mall_title_xpath).click()
        #     d.xpath(confirm_but_xpath).click()
        #     if d.xpath(select_single_box_xpath).exists:
        #         d.xpath(select_single_box_xpath).click()
        #         d.xpath(confirm_but_xpath).click()
        for i in range(1,4,1):
            stop_flag = False
            for j in range(1,5,1):
                if d.xpath(living_add_self_checkBox_xpath % j).exists:
                    if d.xpath(living_add_self_checkBox_xpath % j).info['enabled'] == "true":
                        add_good_name = d.xpath(living_show_win_goods_name_xpath % j).get_text()[8:]
                        stop_flag = True
                        d.xpath(living_add_self_checkBox_xpath % j).click()
                        d.xpath(addGoods_but_xpath).click()
                        break
            if stop_flag:
                break
            d.swipe(0.516, 0.642,0.463, 0.164,0.2)
        time.sleep(0.1)
        if d.xpath(living_show_win_risk_tip_xpath).exists:
            self.add_goods_in_broadcast(d)
        else:
            assert add_good_name == d.xpath(living_show_win_added_goods_name_xpath % 1).get_text()
            d.xpath(living_show_win_checkbox_add_xpath % 1).click()
            time.sleep(0.5)
            d.xpath(living_show_win_finish_btn_xpath).click()
            assert add_good_name == d.xpath(living_show_win_showing_goods_name_xpath % 1).get_text()
            d.press("back")
            return self

    """
    直播间删除商品
    """

    def delete_goods_broadcast(self, d):
        print("delete_goods_broadcast")
        global add_good_name
        d.xpath(living_to_goods_bag_xpath).click()
        time.sleep(1)
        d.xpath(living_baggoods_manager_btn_xpath).click()
        if not d.xpath(living_baggoods_manager_delete_xpath % 1).exists:
            d(text="管理").click()
        d.xpath(living_baggoods_manager_delete_xpath % 1).click()
        assert add_good_name != d.xpath(living_show_win_showing_goods_name_xpath % 1).get_text()[8:]
        d.press("back")
        return self

    """
    开奖
    """
    def draw_lottery(self, d):
        print("draw_lottery")
        d.xpath(living_push_lottery_xpath).click()
        d.xpath(living_draw_lottery_xpath).click()
        # d.xpath(living_back_lottery_xpath).click()
        return self

    """
    发送红包
    """
    def add_red_packet(self, d, amount, number):
        print("add_red_packet")
        d.xpath(living_to_red_packet_btn_xpath).click()
        # 发送红包
        d.xpath(living_red_amount_xpath).click()
        d(focused=True).send_keys(amount)
        if d.xpath(sougou_input_title_xpath).exists or d.xpath(sougou_input_title_math_xpath).exists:
            d.press("back")
        d.xpath(living_red_number_xpath).click()
        d(focused=True).send_keys(number)
        if d.xpath(sougou_input_title_xpath).exists or d.xpath(sougou_input_title_math_xpath).exists:
            d.press("back")
        d.xpath(living_red_buy_xpath).click()
        d.xpath(living_red_confirm_xpath).click()
        d.xpath(living_red_wechat_pay_xpath).wait(timeout=10.0)
        if d.xpath(living_red_wechat_pay_xpath).exists:
            print("to wechat pay successfully")
        else:
            raise Exception("to wechat pay fail")
        # cancel pay
        d.xpath(living_wechat_pay_close_xpath).click()
        d.xpath(living_wechat_pay_giveup_xpath).click()
        d.xpath(living_red_confirm_xpath).wait(timeout=5.0)
        d.press("back")
        return self

    """
    进入更多
    """
    def living_to_menu(self, d):
        print("living_to_menu")
        d.xpath(living_to_menu_btn_xpath).click()
        return self

    """
    创建公告
    """
    def menu_create_notice(self, d, notice_text):
        print("menu_add_notice")
        # 创建公告
        d.xpath(living_menu_to_notice_xpath).click()
        d(focused=True).send_keys(notice_text)
        d.xpath(living_menu_notice_send_xpath).click()
        return self

    """
    查看公告
    """
    def menu_check_notice(self, d, notice_text):
        print("menu_check_notice")
        d.xpath(living_notice_text_xpath).wait(timeout=3.0)
        publish_text = d.xpath(living_notice_text_xpath).get_text()[5:]
        assert publish_text == notice_text
        return self

    """
    创建贴纸
    """
    def menu_create_sticker(self, d, title, content):
        print("menu_check_sticker")
        d.xpath(living_menu_to_sticker_xpath).click()
        d.xpath(living_menu_sticker_first_xpath).click()
        d.xpath(living_menu_sticker_title_xpath).wait(timeout=3.0)
        d.xpath(living_menu_sticker_title_xpath).click()
        d(focused=True).send_keys(title)
        d.xpath(living_menu_sticker_content_xpath).click()
        d(focused=True).send_keys(content)
        time.sleep(1)
        if d.xpath(sougou_input_title_xpath).exists:
            d.press("back")
        d.xpath(living_menu_sticker_confirm2_xpath).wait(timeout=3.0)
        if d.xpath(living_menu_sticker_confirm2_xpath).exists:
            d.xpath(living_menu_sticker_confirm2_xpath).click()
        d.xpath(living_menu_sticker_complete_xpath).click()

        return self

    """
    查看贴纸
    """

    def menu_check_sticker(self, d):
        print("menu_check_sticker")
        d.xpath(living_menu_sticker_showing_xpath).wait(timeout=3.0)
        if d.xpath(living_menu_sticker_showing_xpath).exists:
            print("create sticker successfully")
        else:
            raise Exception("create sticker fail")
        return self

    """
    刪除贴纸
    """

    def menu_delete_sticker(self, d):
        print("menu_delete_sticker")
        d.xpath(living_menu_sticker_showing_xpath).wait(timeout=3.0)
        if d.xpath(living_menu_sticker_showing_xpath).exists:
            print("已存在貼紙")
            d.xpath(living_menu_sticker_showing_xpath).click()
            d.xpath(living_menu_sticker_remove_xpath).wait(timeout=3.0)
            d.xpath(living_menu_sticker_remove_xpath).click()
            d.xpath(living_menu_sticker_ensure_remove_xpath).click()
        return self

    """
    推送关注卡片
    """
    def menu_follow_push(self, d):
        print("menu_follow_push")
        d.xpath(living_menu_to_attention_xpath).click()
        time.sleep(1)
        d.xpath(living_menu_follow_push_xpath).click()
        time.sleep(1)
        d.press("back")
        if d.xpath(living_menu_follow_push_xpath).exists:
            d.press("back")
        return self

    """
    粉丝推送
    """
    def menu_fans_push(self, d):
        print("menu_fans_push")
        time.sleep(2)
        d.xpath(living_menu_to_fans_push_xpath).click()
        d.xpath(ensure_alert_xpath).click()
        time.sleep(2)
        d.press("back")
        return self

    """
    分享微信
    """

    def menu_share_wechat(self, d):
        print("menu_share_wechat")
        d.xpath(living_menu_to_share_xpath).click()
        d.xpath(share_wechat_icon_xpath).click()
        d.xpath(wechat_group_head_xpath).wait(timeout=5.0)
        d.xpath(wechat_group_head_xpath).click()
        d.xpath(wechat_send_confirm_xpath).click()
        d.xpath(wechat_sent_back_xpath).click()
        return self

    """
    下载海报
    """
    def menu_share_wechat_download(self, d):
        print("menu_share_wechat_download")
        d.xpath(living_menu_to_share_xpath).click()
        d.xpath(download_card_xpath).click()
        time.sleep(2)
        d.press("back")
        return self

    """
    分享朋友圈
    """

    def menu_share_wechat_list(self, d):
        print("menu_share_wechat_list")
        d.xpath(living_menu_to_share_xpath).click()
        d.xpath(share_wechat_list_icon_xpath).wait(timeout=5.0)
        d.xpath(share_wechat_list_icon_xpath).click()
        d.xpath(wechat_list_send_xpath).wait(timeout=5.0)
        d.xpath(wechat_list_send_xpath).click()
        return self

    def living_stop(self, d):
        d.xpath(end_live_broadcast_btn_xpath).click()
        d.xpath(ensure_alert_xpath).click()
        d.xpath(end_broadcast_title_xpath).wait(timeout=3.0)
        assert True == d.xpath(end_broadcast_title_xpath).exists
        d.xpath(end_broadcast_back_xpath).click()
        return self